<?php

namespace app\api\controller;

class Index
{

    /**
     * 首页展示
     */
    public function index()
    {

        $private_key = 'MIIEpAIBAAKCAQEAtzJCbI9DAQdR858OTdW3MW8anVrMGRRQXq2voBY2BpSZDouX4lNFva/7+V4G3he9QdgqZyUHrS4QJ1KYVrQepNEPhvLvnxTR1PzpmsU+VsvmG26BuThkGTAcSC/s7GCPBZaV0/4yNcLjjJH5uyRTLXoH7Zv0Q+bJ11uMze5WFR5eAHlQWr/Mij+iMG9XXK/2uoG2RTvQiQA/0xDpr1H6hq8+yeXKYey8QAWIDwaPh5gA/s0oz+MDzN0M8U4ZT5j94jFUjuJLLq6wAJhX4DipCcejaZAFU7gD2ru2ZlKlJRyH84ZimPDAMUZV62tMxJ9jj00sy3CEtU7aempqTuKoTwIDAQABAoIBADP/lg8OUux10XMfEIZTRR5I9Pr5DLmJpI5bKmB8LXWMtC0GvmPFUyRWUWUkpTpg7p1D6DVCs30aeAdRUGA72/7YQFoDlrRGypB0ATKnDdzBH11l7nhOY0sq9rwerlchyTbC2WBE5KeR29jIjmPqGZns61csTxb+6QexurOxFMoFSzQeiAjPtRbpWYnM3zLe9R76/rrInNcxXxMTcxFHEbZvFTMbSoHX10q5xW3vRmGnMBN+UXrzRzacAhs/Iu4seNTg1E+7NNNr4lgW8yChPIXnp6ewnTQrRXukMOI9jaGx681D1CvARF8E3n4kK9T/eagnzaqsUgdZUZ82Gx18gUECgYEA67HKHSjyphvwRg0482KvQMfJlQYhZS7pIGbvX0sXgx0j112VCZrJo+1GczK6jAkejQUJffeTo40g/gLs4gSoWPyvGrj3iHysUA7NbVN0QopyPQZWxpmnKnGm9SJMAVZT+GkziJKBSRoNeosEMTdVxBQdvS1EBHRt0VSbl/O4FvECgYEAxvqhRVB3cSZbPnp8NHlMe/TBIlm6VqgXF28t0HUZQkxGkluaBuu2Ks++1OYkC2h9HhBpPOWfByfTuBNv92RFLwxLaM+s/TrnS2JuJNLkpH467ZEMF+9RbNfzsQVK+8jYLHclASuBDSIo2EChipE7qMoTh3l8ECAicl1FuHYzMz8CgYB2R9XPRnJnTBQLg6017ytWDi0qnVX7CkyyFxY/s1aMVP5coTkfBc/1dSjqvOWDSzxKtuFpluo/pJ6O+vbYAlBdwAk8FuEaXL64uoT7PWtVzVbBK0I3DnsmwLxxvpF4BlPDkT/sZOF+xxVGjyc+b6+Z5CJ8go/k0G0gJR6kZgnXcQKBgQDBjnYoXgECUWR6311JEiGjhtAE4THRMAveYOk5GV+rzVTs9CxdxRbciF/C6+huLINhJmeotFlA9a4PJuFIzwI5pQUJ/NLzNkReYW5xZbDCjoQ2Kl2QPt1/Ooa0UUjCqnoWrwORIseThgIjcqNxjptxMrlSlItpiEjZ5P94U4q0swKBgQCkgcrCdZtBELO5pTkLb9pRNjuRnqKznnoEYY0Vv5GJl6YaBF/HnjXWvVSOAMiQqMHkvjTlExP2Jjz5Lddrr3hzrkwYkWEFWY87efc/FskJ6HbJAXr912T5ESlX959VC7w4UbY/Ne2+YwhSedqvPGoeCJIl9CEhq115UCTFXofjww==';
        $search = [
            "-----BEGIN RSA PRIVATE KEY-----",
            "-----END RSA PRIVATE KEY-----",
            "\n",
            "\r",
            "\r\n"
        ];

        $private_key=str_replace($search,"",$private_key);
        $private_key=$search[0] . PHP_EOL . wordwrap($private_key, 64, "\n", true) . PHP_EOL . $search[1];

        $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtzJCbI9DAQdR858OTdW3MW8anVrMGRRQXq2voBY2BpSZDouX4lNFva/7+V4G3he9QdgqZyUHrS4QJ1KYVrQepNEPhvLvnxTR1PzpmsU+VsvmG26BuThkGTAcSC/s7GCPBZaV0/4yNcLjjJH5uyRTLXoH7Zv0Q+bJ11uMze5WFR5eAHlQWr/Mij+iMG9XXK/2uoG2RTvQiQA/0xDpr1H6hq8+yeXKYey8QAWIDwaPh5gA/s0oz+MDzN0M8U4ZT5j94jFUjuJLLq6wAJhX4DipCcejaZAFU7gD2ru2ZlKlJRyH84ZimPDAMUZV62tMxJ9jj00sy3CEtU7aempqTuKoTwIDAQAB';
        $search = [
            "-----BEGIN PUBLIC KEY-----",
            "-----END PUBLIC KEY-----",
            "\n",
            "\r",
            "\r\n"
        ];
        $public_key=str_replace($search,"",$public_key);
        $public_key=$search[0] . PHP_EOL . wordwrap($public_key, 64, "\n", true) . PHP_EOL . $search[1];

        $pi_key =  openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的，可用返回资源id Resource id
        $pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的



        $data = "123456";//原始数据
        $encrypted = "";
        $decrypted = "";


        openssl_public_encrypt($data,$encrypted,$pu_key,OPENSSL_PKCS1_PADDING);//公钥加密
        $encrypted = base64_encode($encrypted);
        echo $encrypted,"\n";
        echo "<hr>";
        echo "private key decrypt:\n";
        openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key,OPENSSL_PKCS1_PADDING);//私钥解密

        echo $decrypted."\n";
    }


}
